package com.adm.biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yfsoft.comm.util.BizResult;
import org.yfsoft.comm.util.StringUtil;

import com.adm.ctl.ResultType;
import com.adm.dao.CommonDao;

public class TplService extends Service {

	private CommonDao dao;
	@Override
	public void setDao(CommonDao dao) {
		this.dao = dao;
	}
	
	
	public BizResult list(Map<String,String> input){
		String m_id = input.get("m_id");
		Object m = this.dao.query("sys_model",null,"m_id = "+m_id).getData();
		this.setReqAttr("model", m);
		BizResult rst = this.dao.injoin(new String[]{"sys_model_control mc","sys_control c"}, 
				new String[]{"mc.*","c.*"},
				"mc.mc_control_id = c.c_id",
				"mc.mc_model_id = "+m_id,
				null,100,0);
		if(((List)rst.getData()).size()<1){
			this.setReqAttr("msg", "未设置栏位信息");
			rst.setType(String.valueOf(ResultType.REDIRECT));
			rst.setPage("tpl/list.jsp");
			return rst;
		}else{
			this.setReqAttr("controls", rst.getData());
			Object entrys = this.dao.list(((Map<String,Object>)m).get("m_view").toString(),null,null,null,100,0).getData();
			List<Map<String,Object>> entrys_list = (List<Map<String,Object>>)entrys;
			for(Map<String,Object> e : entrys_list){
				
			}
			this.setReqAttr("es", entrys_list);
		}
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("tpl/list.jsp");
		return rst;
	}
	
	public List<Map<String,Object>> getDataSource(Object ds_id) throws Throwable{
		BizResult ds_rst = this.dao.query("sys_ds",null,"ds_id = "+ds_id);
		if(!"0".equals(ds_rst.getCode())){
			throw new Exception("数据源获取错误!");
		}
		Map<String,Object> ds = (Map<String,Object>)ds_rst.getData();
		if(null!=ds && ds.size()>0){
			String ds_type_val = ds.get("ds_type").toString();
			List<Map<String,Object>> mc_ds = null;
			if("map".equals(ds_type_val)){
				mc_ds = new ArrayList<Map<String,Object>>();
				Map<String,Object> data ;
				//1:是,0:否
				String ds_map = ds.get("ds_collection").toString();
				String[] ds_map_split = ds_map.split(",");
				for(String s : ds_map_split){
					data = new HashMap<String,Object>();
					data.put("k", StringUtil.between(s, ":", StringUtil.BetweenPos.END));
					data.put("v", StringUtil.between(s, ":", StringUtil.BetweenPos.START));
					mc_ds.add(data);
				}
			}else if("sql".equals(ds_type_val)){
				String sql = ds.get("ds_sql").toString();
				mc_ds = (List<Map<String,Object>>)this.dao.list(sql).getData();
			}
			return mc_ds;
		}
		throw new Exception("未找到任何数据源!");
	}
	
	
	public Object getDataValue(int ds_id,Object k) throws Throwable{
		BizResult ds_rst = this.dao.query("sys_ds",null,"ds_id = "+ds_id);
		if(!"0".equals(ds_rst.getCode())){
			throw new Exception("数据源获取错误!");
		}
		Map<String,Object> ds = (Map<String,Object>)ds_rst.getData();
		if(null!=ds && ds.size()>0){
			String ds_type_val = ds.get("ds_type").toString();
			if("map".equals(ds_type_val)){
				String ds_map = ds.get("ds_collection").toString();
				String[] ds_map_split = ds_map.split(",");
				for(String s : ds_map_split){
					if(k.equals(StringUtil.between(s, ":", StringUtil.BetweenPos.END)))
						return StringUtil.between(s, ":", StringUtil.BetweenPos.START);
				}
				throw new Exception("未找到任何数据!");
			}else if("sql".equals(ds_type_val)){
				String sql = "select v from ({0}) as t where k = {1}";
				sql = StringUtil.formatSql(sql, ds.get("ds_sql").toString(),k);
				System.out.println(sql);
				{
					Object v_map = this.dao.query(sql).getData();
					if(null == v_map)
						throw new Exception("未找到任何数据!");
					return ((Map<String,Object>)v_map).get("v");
				}
			}
		}
		throw new Exception("未找到任何数据!");
	}
	/**
	 * 添加模型对象
	 * @param input 参数列表
	 * @return
	 * TODO:关联的数据集需要采集
	 */
	public BizResult form(Map<String,String> input){
		String m_id = input.get("m_id");
		Object obj = this.dao.query("sys_model",null,"m_id = "+m_id).getData();
		this.setReqAttr("model", obj);
		BizResult rst = this.dao.injoin(new String[]{"sys_model_control mc","sys_control c"}, 
				new String[]{"mc.*","c.*"},
				"mc.mc_control_id = c.c_id",
				"mc.mc_model_id = "+m_id,
				null,100,0);
		obj = rst.getData();
		List<Map<String,Object>> controls = (List<Map<String,Object>>)obj;
		for(int i = 0 ; i<controls.size() ; i++){
			Map<String,Object> c = controls.get(i);
			//不需要提取数据源字段
			if(!c.containsKey("mc_ds")) continue;
			Object mc_ds_val = c.get("mc_ds");
			//未绑定数据源
			if(null == mc_ds_val) continue;
			try {
				c.put("mc_ds", getDataSource(mc_ds_val));
			} catch (Throwable e) {
				this.setReqAttr("msg", e.getMessage());
			}
		}
		this.setReqAttr("controls", controls);
		String p = input.get("p");
		if("add".equals(p)){
			//添加表单
			this.setReqAttr("p", "add");
		}else if("edit".equals(p)){
			//修改表单
			//TODO..加载属性
			String e_id = input.get("key");
			String pri = input.get("pri");
			String v = input.get("v");
			this.setReqAttr("e", this.dao.query(v,null,pri+"="+e_id).getData());
			this.setReqAttr("p", "edit");
			this.setReqAttr("pri", pri);
			this.setReqAttr("key", e_id);
			this.setReqAttr("v",v);
		}
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("tpl/form.jsp");
		return rst;
	}
	
	public BizResult delete(Map<String,String> input){
		String key = input.get("key");
		String pri = input.get("pri");
		BizResult rst = this.dao.delete(input.get("v"),pri+"="+key);
		if("0".equals(rst.getCode())){
			this.setReqAttr("msg", "OK");
		}else{
			this.setReqAttr("msg", rst.getMsg());
		}
		return list(input);
	}
	
	public BizResult edit(Map<String,String> input){
		String key = input.get("key");
		String pri = input.get("pri");
		String v = input.get("v");
		BizResult rst = this.dao.update(input, pri+"="+key, v);
		if("0".equals(rst.getCode())){
			this.setReqAttr("msg", "OK");
		}else{
			this.setReqAttr("msg", rst.getMsg());
		}
		return list(input);
	}
	
	public BizResult add(Map<String,String> input){
		String m_id = input.get("m_id");
		input.remove("m_id");
		Object m = this.dao.query("sys_model",null,"m_id = "+m_id).getData();
		Map<String,String> model = (Map<String,String>)m;
		this.setReqAttr("model", m);
		this.dao.insert(input,model.get("m_view") );
		input.put("m_id", m_id);
		return list(input);
	}

}
